package priv.cwr.springboot.demo.controller;

import org.joda.time.DateTime;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import priv.cwr.springboot.demo.model.db.DeptEntity;
import priv.cwr.springboot.demo.model.vo.DeptAddForm;
import priv.cwr.springboot.demo.model.vo.base.Result;
import priv.cwr.springboot.demo.service.DeptService;

import javax.validation.Valid;
import java.util.Date;

/**
 * User: R
 * Date: 2018/8/29
 * Time: 11:01
 * Created with IntelliJ IDEA.
 */
@RestController
@RequestMapping("/dept")
@Validated
public class DeptController {
    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @PostMapping("/add")
    public ResponseEntity<Result<Long>> addDept(@Valid @RequestBody DeptAddForm deptAddForm,
                                                BindingResult bindingResult) {
        Date curTime = DateTime.now().toDate();
        DeptEntity deptEntity = new DeptEntity();
        deptEntity.setName(deptAddForm.getName());
        deptEntity.setParentId(deptAddForm.getParentId());
        deptEntity.setCreateTime(curTime);
        long id = this.deptService.addDept(deptEntity);
        return new ResponseEntity<>(new Result<>(id), HttpStatus.OK);
    }

}
